package com.example.a10928.weifriend.ui.home.menu


import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.a10928.weifriend.App
import com.example.a10928.weifriend.R
import com.example.a10928.weifriend.adapter.ActiveCenterAdapter
import com.example.a10928.weifriend.adapter.AdertAdapter
import com.example.a10928.weifriend.adapter.MyTeamAdapter
import com.example.a10928.weifriend.http.api.VariableName
import com.example.a10928.weifriend.http.api.base.ActivityShopBean
import com.example.a10928.weifriend.http.api.base.MyAdertBean
import com.example.a10928.weifriend.ui.BaseFragment
import com.example.a10928.weifriend.ui.task_center.TaskDetailsActivity
import com.example.a10928.weifriend.util.RxUtil
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.fragment_my_adert.*
import java.util.ArrayList

class ActiveCenterFragment : BaseFragment(){



    var datas = ArrayList<ActivityShopBean.DataBean>()
    var adapter :ActiveCenterAdapter?=null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        return inflater.inflate(R.layout.fragment_my_adert, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        var type=arguments?.getString(VariableName.TYPE)


        adapter = ActiveCenterAdapter(R.layout.item_active_center, datas,type)
        recyc.setLayoutManager(LinearLayoutManager(activity))
        recyc.setAdapter(adapter)

        adapter?.setOnItemClickListener { adapter, view, position ->

            var myIntent=Intent(activity,ActiveDetailActivity::class.java)

            myIntent.putExtra(VariableName.TYPE,type)
            myIntent.putExtra(VariableName.DATA,datas.get(position).id)
            myIntent.putParcelableArrayListExtra(VariableName.IMG,datas.get(position).images )
            startActivity(myIntent)

        }

        if(type.equals("1")){
            App.app().bpService.get_shop_activity(App.app().user.userId,App.app().user.token,"1")
                    .compose(RxUtil.io2main())
                    .subscribe(
                            Consumer {
                                datas.clear()
                                datas.addAll(it.data)
                                adapter?.notifyDataSetChanged()
                            },
                            Consumer {
                                RxUtil.errorException(it)
                            }
                    )
        }else if(type.equals("2")){
            App.app().bpService.get_shop_activity(App.app().user.userId,App.app().user.token,"0")
                    .compose(RxUtil.io2main())
                    .subscribe(
                            Consumer {
                                datas.clear()
                                datas.addAll(it.data)
                                adapter?.notifyDataSetChanged()
                            },
                            Consumer {
                                RxUtil.errorException(it)
                            }
                    )
        }else{
            App.app().bpService.get_my_activity(App.app().user.userId,App.app().user.token)
                    .compose(RxUtil.io2main())
                    .subscribe(
                            Consumer {
                                datas.clear()
                                datas.addAll(it.data)
                                adapter?.notifyDataSetChanged()
                            },
                            Consumer {
                                RxUtil.errorException(it)
                            }
                    )
        }
    }


}
